/*
 * Project Name: jmax
 * File Name: FilterGroupServiceImpl.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.sys.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.core.model.BaseItem;
import com.xdtech.sys.dao.FilterGroupDao;
import com.xdtech.sys.model.FilterGroup;
import com.xdtech.sys.model.FilterRule;
import com.xdtech.sys.model.FilterTranslator;
import com.xdtech.sys.service.FilterGroupService;
import com.xdtech.sys.service.FilterRuleService;
import com.xdtech.web.model.Pagination;

/**
 * TODO 一句话功能简述，请确保和下面的block tags之间保留一行空行
 * <p>
 * TODO 功能详细描述，若不需要请连同上面的p标签一起删除
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-3-19 上午9:26:58
 */
@Service
public class FilterGroupServiceImpl implements FilterGroupService
{
	@Autowired
	private FilterGroupDao filterGroupDao;
	@Autowired
	private FilterRuleService filterRuleService;
	/* (非 Javadoc)
	 * save
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param entity
	 * @see com.xdtech.common.service.IBaseService#save(com.xdtech.core.model.BaseModel)
	 */
	@Override
	public void save(FilterGroup entity)
	{
		filterGroupDao.save(entity);
	}

	/* (非 Javadoc)
	 * saveAll
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param entities
	 * @see com.xdtech.common.service.IBaseService#saveAll(java.util.List)
	 */
	@Override
	public void saveAll(List<FilterGroup> entities)
	{
		filterGroupDao.saveAll(entities);
	}

	/* (非 Javadoc)
	 * delete
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param entity
	 * @see com.xdtech.common.service.IBaseService#delete(com.xdtech.core.model.BaseModel)
	 */
	@Override
	public void delete(FilterGroup entity)
	{
		//先删除子表rule里记录
		filterRuleService.deleteByGroupId(entity.getId());
		filterGroupDao.delete(entity);
	}

	/* (非 Javadoc)
	 * delete
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param id
	 * @see com.xdtech.common.service.IBaseService#delete(java.lang.Long)
	 */
	@Override
	public void delete(Long id)
	{
		filterGroupDao.delete(id);
	}

	/* (非 Javadoc)
	 * get
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param id
	 * @return
	 * @see com.xdtech.common.service.IBaseService#get(java.lang.Long)
	 */
	@Override
	public FilterGroup get(Long id)
	{
		return filterGroupDao.get(id);
	}

	/* (非 Javadoc)
	 * getAll
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @return
	 * @see com.xdtech.common.service.IBaseService#getAll()
	 */
	@Override
	public List<FilterGroup> getAll()
	{
		return filterGroupDao.getAll();
	}

	/* (非 Javadoc)
	 * loadPageAndCondition
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param pg
	 * @param values
	 * @return
	 * @see com.xdtech.common.service.IBaseService#loadPageAndCondition(com.xdtech.web.model.Pagination, java.util.Map)
	 */
	@Override
	public Map<String, Object> loadPageAndCondition(Pagination pg, Map<String, String> values)
	{
		return null;
	}

	/* (非 Javadoc)
	 * deleteByTranslator
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param filterTranslator
	 * @see com.xdtech.sys.service.FilterGroupService#deleteByTranslator(com.xdtech.sys.model.FilterTranslator)
	 */
	@Override
	public void deleteByTranslator(FilterTranslator filterTranslator)
	{
		List<FilterGroup> groups = filterGroupDao.findByHql("from FilterGroup where translator.id=?", filterTranslator.getId());
		for (FilterGroup filterGroup : groups)
		{
			delete(filterGroup);
		}
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-3-22上午12:06:05
	 * @modified by
	 * @param groups
	 */
	@Override
	public void saveGroupsLinkRule(FilterTranslator translator,List<FilterGroup> groups) {
		for (FilterGroup filterGroup : groups) {
			filterGroup.setTranslator(translator);
			List<FilterRule> filterRules = filterGroup.getFilterRules();
			save(filterGroup);
			for (FilterRule filterRule : filterRules) {
				filterRule.setFilterGroup(filterGroup);
				filterRuleService.save(filterRule);
			}
			
		}
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:24:27
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
